package com.example.skillcinema.ui.screens.profile.viewModels

import android.app.Dialog
import android.widget.EditText
import android.widget.ImageButton
import androidx.appcompat.widget.AppCompatButton
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.skillcinema.R
import com.example.skillcinema.db.displaymodels.FilmDisplayModel
import com.example.skillcinema.db.displaymodels.ProfileCollection
import com.example.skillcinema.db.localdb.tables.CollectionEntity
import com.example.skillcinema.db.repositories.FilmForPreviewRepository
import com.example.skillcinema.db.repositories.FilmRepository
import com.example.skillcinema.extensions.sliceForPreview
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class ProfileViewModel @Inject constructor(val repository: FilmForPreviewRepository) : ViewModel() {
    private val _collectionsStateFlow: MutableStateFlow<List<ProfileCollection>> =
        MutableStateFlow(listOf())
    val collectionsFlow = _collectionsStateFlow.asStateFlow()
    private val _watchedStateFlow: MutableStateFlow<List<FilmDisplayModel>> =
        MutableStateFlow(listOf())
    val watchedFlow = _watchedStateFlow.asStateFlow()
    private val _historyStateFlow: MutableStateFlow<List<FilmDisplayModel>> =
        MutableStateFlow(listOf())
    val historyFlow = _historyStateFlow.asStateFlow()

    init {
        loadCollections()
        loadWatched()
        loadHistory()
    }

    fun loadHistory() {
        viewModelScope.launch {
            _historyStateFlow.value = repository.db.historyDao().getAllHistory().sliceForPreview()
                .map { repository.getFilmById(it.elementId) }
        }
    }

    fun loadWatched() {
        viewModelScope.launch {
            _watchedStateFlow.value =
                repository.db.filmCollectionCrossRefDao().getFilmsByCollectionId(3)
                    .sliceForPreview().map {
                        repository.getFilmById(it.filmId)
                    }
        }
    }

    fun loadCollections() {
        viewModelScope.launch {
            _collectionsStateFlow.value =
                repository.db.collectionDao().getAllCollections().sliceForPreview().map {
                    ProfileCollection(
                        it.collectionId,
                        it.type,
                        it.name,
                        getCount(it.collectionId),
                        it.isSystem
                    )
                }
        }
    }

    fun removeCollection(id: Int) {
        viewModelScope.launch {
            val collection: CollectionEntity? = repository.db.collectionDao().getCollectionById(id)
            if (collection != null)
                repository.db.collectionDao()
                    .deleteCollection(collection)
            loadCollections()
        }
    }

    fun createCollection(name: String) {
        viewModelScope.launch {
            repository.db.collectionDao().insertCollection(
                CollectionEntity(
                    0, name,
                    CollectionEntity.CollectionType.CUSTOM
                )
            )
            loadCollections()
        }
    }

    fun clearWatched() {
        viewModelScope.launch {
            repository.db.filmCollectionCrossRefDao().clearCollection(3)
            loadWatched()
        }
    }

    fun clearHistory() {
        viewModelScope.launch {
            repository.db.historyDao().clearHistory()
            loadHistory()
        }
    }

    private suspend fun getCount(id: Int): Int {
        val count = repository.db.filmCollectionCrossRefDao()
            .getFilmsByCollectionId(id).count()
        return count
    }
}